********************
* Loading datasets *
********************

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180222_1153_T2_prepared.txt", encoding(ISO-8859-1) clear
gen session=1
gen pr=0
gen fixed=0
gen T=2
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data1.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180216_1154_T2_prepared.txt", encoding(ISO-8859-1) clear
gen session=2
replace pr=0
gen fixed=0
gen T=2
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data2.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180209_1134_T1_prepared.txt", encoding(ISO-8859-1) clear
gen session=3
replace pr=1
gen fixed=0
gen T=1
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data3.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180302_1231_T1_prepared.txt", encoding(ISO-8859-1) clear
gen session=4
replace pr=1
gen fixed=0
gen T=1
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data4.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180503_1151_T1_prepared.txt", encoding(ISO-8859-1) clear
gen session=5
replace pr=1
gen fixed=0
gen T=1
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data5.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180502_1136_T2_prepared.txt", encoding(ISO-8859-1) clear
gen session=6
replace pr=0
gen fixed=0
gen T=2
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data6.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/180509_1554_T5_prepared.txt", encoding(ISO-8859-1) clear
gen session=7
gen pr=1
gen fixed=1
gen T=5
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data7.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T3_190314_1201_prepared.txt", encoding(ISO-8859-1) clear
gen session=8
gen pr=1
gen fixed=1
gen T=3
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data8.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T4_190314_1326_prepared.txt", encoding(ISO-8859-1) clear
gen session=9
gen pr=0
gen fixed=1
gen T=4
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data9.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T3_190322_1217_prepared.txt", encoding(ISO-8859-1) clear
gen session=10
gen pr=1
gen fixed=1
gen T=3
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data10.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T4_190322_1425_prepared.txt", encoding(ISO-8859-1) clear
gen session=11
gen pr=0
gen fixed=1
gen T=4
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data11.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T6_190326_1427_prepared.txt", encoding(ISO-8859-1) clear
gen session=12
gen pr=0
gen fixed=1
gen T=6
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data12.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T5_190327_1423_prepared.txt", encoding(ISO-8859-1) clear
gen session=13
gen pr=1
gen fixed=1
gen T=5
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data13.dta", replace

import delimited "/Users/damienbol/Dropbox/Duverger in the lab/data/T6_190328_1025_prepared.txt", encoding(ISO-8859-1) clear
gen session=14
gen pr=0
gen fixed=1
gen T=6
save "/Users/damienbol/Dropbox/Duverger in the lab/data/data14.dta", replace

use "/Users/damienbol/Dropbox/Duverger in the lab/data/data1.dta", clear
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data2.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data3.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data4.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data5.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data6.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data7.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data8.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data9.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data10.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data11.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data12.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data13.dta", force
append using "/Users/damienbol/Dropbox/Duverger in the lab/data/data14.dta", force

**************************
* Creating new variables *
**************************

* Entry variable

gen entry=1
replace entry=0 if votes==-100
replace entry=0 if votes==100

* Election id

bysort session period group: gen group_id=1 if _n==1

* Number of entries

bysort session period group: egen n_entries=total(entry)
replace n_entries=2 if T==5
replace n_entries=2 if T==6
replace n_entries=3 if T==3
replace n_entries=3 if T==4

* Fixing bug at session 12

set seed 12345

gen random=runiform() if group==1 & p1==0 & session==12
bysort period:egen max_random=max(random)
replace group=3 if max_random==random

* Proposals

replace proposal=. if entry==0

gen proposal_p1=proposal if p1==0
gen proposal_p2=proposal if p1==5
gen proposal_p3=proposal if p1==10

bysort session period group: egen proposal_p1_all=max(proposal_p1)
bysort session period group: egen proposal_p2_all=max(proposal_p2)
bysort session period group: egen proposal_p3_all=max(proposal_p3)

* Average proposal

gen average_proposal=(proposal_p1_all+proposal_p2_all+proposal_p3_all)/n_entries if T<5
replace average_proposal=(proposal_p1_all+proposal_p3_all)/n_entries if T>4

* Winning proposal

gen winning_proposal=final_food
replace winning_proposal=final_foodx if final_food==.

* Polarization

gen max_proposal=max(proposal_p1_all, proposal_p2_all, proposal_p3_all) if T<5
gen min_proposal=min(proposal_p1_all, proposal_p2_all, proposal_p3_all) if T<5

replace max_proposal=max(proposal_p1_all, proposal_p3_all) if T>4
replace min_proposal=min(proposal_p1_all, proposal_p3_all) if T>4

gen polarization=max_proposal-min_proposal

* Series id

gen series=0
replace series=1 if period<6
replace series=2 if period>5 & period<11
replace series=3 if period>10 & period<16
replace series=4 if period>15 & period<21
replace series=5 if period>20

egen cluster_id=group(group series session)

* Creating a variable capturing last 2 elections of each series

gen last_election=0
replace last_election=1 if period==4
replace last_election=1 if period==5
replace last_election=1 if period==9
replace last_election=1 if period==10
replace last_election=1 if period==14
replace last_election=1 if period==15
replace last_election=1 if period==19
replace last_election=1 if period==20
replace last_election=1 if period==24
replace last_election=1 if period==25

**************************
* Description of results *
**************************

* Table 3: Number of entries
bysort T: tab n_entries if group_id==1

* Table 3: Average proposal
bysort T: sum average_proposal if group_id==1

* Figure 3: Average proposal
twoway histogram proposal if T==1, percent width(1) start(0) lp(solid) lcolor(black) xlabel(0(1)10) recast(line) || histogram proposal if T==2, percent width(1) start(0) lp(dash) lcolor(black) xlabel(0(1)10) recast(line) graphregion(color(white)) xtitle(Proposal) ylabel(0(10)40) legend(order(1 "PR" 2 "Plu.")) title("Endogenous (T1-T2)", color(black))
graph save "/Users/damienbol/Desktop/p1.gph", replace

twoway histogram proposal if T==3, percent width(1) start(0) lp(solid) lcolor(black) xlabel(0(1)10) recast(line) ytitle("") || histogram proposal if T==4, percent width(1) start(0) lp(dash) lcolor(black) xlabel(0(1)10) recast(line) graphregion(color(white)) xtitle(Proposal) ylabel(0(10)40) ytitle("") legend(order(1 "PR" 2 "Plu.")) title("3 parties (T3-T4)", color(black))
graph save "/Users/damienbol/Desktop/p2.gph", replace

twoway histogram proposal if T==5, percent width(1) start(0) lp(solid) lcolor(black) xlabel(0(1)10) recast(line)  ytitle("") || histogram proposal if T==6, percent width(1) start(0) lp(dash) lcolor(black) xlabel(0(1)10) recast(line) graphregion(color(white)) xtitle(Proposal) ylabel(0(10)40)  ytitle("") legend(order(1 "PR" 2 "Plu.")) title("2 parties (T5-T6)", color(black))
graph save "/Users/damienbol/Desktop/p3.gph", replace

graph combine "/Users/damienbol/Desktop/p1.gph" "/Users/damienbol/Desktop/p2.gph" "/Users/damienbol/Desktop/p3.gph", row(1) graphregion(color(white))

********************************
* Hypotheses testing (Table 4) *
********************************

* H1
ttest polarization if group_id==1 & fixed==0 & n_entries==2, by(pr) unequal
ttest polarization if group_id==1 & fixed==0 & n_entries==3, by(pr) unequal

* H2
ttest n_entries if group_id==1 & fixed==0, by(pr) unequal
ttest n_entries if n_entries!=0 & n_entries!=1 & group_id==1 & fixed==0, by(pr) unequal

* H3
ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal

********************************
* Hypotheses testing (Table 5) *
********************************

* 2 entries without P5
ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=., by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=., by(n_entries) unequal

* 2 entries with P5
ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=., by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=., by(n_entries) unequal

******************************************
* Individidual-level decisions (Table 6) *
******************************************

* Entries
ttest entry if p1==0 & fixed==0, by(pr) unequal
ttest entry if p1==5 & fixed==0, by(pr) unequal
ttest entry if p1==10 & fixed==0, by(pr) unequal

* Proposals
ttest proposal_p1_all if group_id==1 & fixed==0, by(pr) unequal
ttest proposal_p2_all if group_id==1 & fixed==0, by(pr) unequal
ttest proposal_p3_all if group_id==1 & fixed==0, by(pr) unequal

********************************
* Hypotheses testing (Table 7) *
********************************

* Table
ttest polarization if group_id==1 & fixed==1 & n_entries==2, by(pr) unequal
ttest polarization if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal

ttest polarization if group_id==1 & fixed==1 & pr==0, by(n_entries)
ttest polarization if group_id==1 & fixed==1 & pr==1, by(n_entries)

****************************************
* Individual-level decisions (Table 8) *
****************************************

* 2 entries
ttest proposal_p1_all if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal
ttest proposal_p3_all if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal

* 3 entries
ttest proposal_p1_all if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal
ttest proposal_p2_all if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal
ttest proposal_p3_all if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal

**********************************
* Calculating payments (in text) *
**********************************

gen real_pay=0
replace real_pay=points if payperiod==period
replace real_pay=points/10*0.7
replace real_pay=real_pay+5
sum real_pay if period==1

**************************
* Supplementary analysis *
**************************

* Hypotheses testing with alternative measure

gen max_proposal_extreme=abs(5-max_proposal)
gen min_proposal_extreme=abs(5-min_proposal)

gen avg_extreme=(max_proposal_extreme+min_proposal_extreme)/2

* H1 (endogenous)
ttest avg_extreme if group_id==1 & fixed==0 & n_entries==2, by(pr) unequal
ttest avg_extreme if group_id==1 & fixed==0 & n_entries==3, by(pr) unequal

* H3 (endogenous)
ttest avg_extreme if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal
ttest avg_extreme if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal

* H1 (fixed)
ttest avg_extreme if group_id==1 & fixed==1 & n_entries==2, by(pr) unequal
ttest avg_extreme if group_id==1 & fixed==1 & n_entries==3, by(pr) unequal

* H3 (fixed)
ttest avg_extreme if fixed==1 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal
ttest avg_extreme if fixed==1 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1, by(n_entries) unequal

* Breakdown by period

* H1 (endogenous)
ttest polarization if group_id==1 & fixed==0 & n_entries==2 & period<16, by(pr) unequal
ttest polarization if group_id==1 & fixed==0 & n_entries==3 & period<16, by(pr) unequal

ttest polarization if group_id==1 & fixed==0 & n_entries==2 & period>15, by(pr) unequal
ttest polarization if group_id==1 & fixed==0 & n_entries==3 & period>15, by(pr) unequal

* H2 (endogenous)
ttest n_entries if group_id==1 & fixed==0 & period<16, by(pr) unequal
ttest n_entries if n_entries!=0 & n_entries!=1 & group_id==1 & fixed==0 & period<16, by(pr) unequal

ttest n_entries if group_id==1 & fixed==0 & period>15, by(pr) unequal
ttest n_entries if n_entries!=0 & n_entries!=1 & group_id==1 & fixed==0 & period>15, by(pr) unequal

* H3 (endogenous)
ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & period<16, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & period<16, by(n_entries) unequal

ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & period>15, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & period>15, by(n_entries) unequal

* H1 (fixed)
ttest polarization if group_id==1 & fixed==1 & n_entries==2 & period<16, by(pr) unequal
ttest polarization if group_id==1 & fixed==1 & n_entries==3 & period<16, by(pr) unequal

ttest polarization if group_id==1 & fixed==1 & n_entries==2 & period>15, by(pr) unequal
ttest polarization if group_id==1 & fixed==1 & n_entries==3 & period>15, by(pr) unequal

* H3 (endogenous)
ttest polarization if fixed==1 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & period<16, by(n_entries) unequal
ttest polarization if fixed==1 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & period<16, by(n_entries) unequal

ttest polarization if fixed==1 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & period>15, by(n_entries) unequal
ttest polarization if fixed==1 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & period>15, by(n_entries) unequal

* Table 5 (endogenous)
ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=. & period<16, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=. & period<16, by(n_entries) unequal

ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=. & period>15, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p3_all!=. & proposal_p1_all!=. & period>15, by(n_entries) unequal

ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=. & period<16, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=. & period<16, by(n_entries) unequal

ttest polarization if fixed==0 & pr==0 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=. & period>15, by(n_entries) unequal
ttest polarization if fixed==0 & pr==1 & group_id==1 & n_entries!=0 & n_entries!=1 & proposal_p2_all!=. & period>15, by(n_entries) unequal

